Skip to content

Fix NativeAOT NullReferenceException in GNOME views#120

Merged
nlogozzo merged 2 commits intomainfrom
copilot/fix-null-reference-exception
Mar 21, 2026
Merged

Fix NativeAOT NullReferenceException in GNOME views#120
nlogozzo merged 2 commits intomainfrom
copilot/fix-null-reference-exception

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 21, 2026

In NativeAOT mode, the trimmer strips private field reflection metadata by default. Gtk.Builder.ConnectFields() uses Type.GetFields(BindingFlags.NonPublic | ...) to populate [Gtk.Connect]-annotated fields — in NativeAOT this returns an empty array, leaving all connected widgets as null and causing a NullReferenceException on first access.

Changes

  • MainWindow.cs / PreferencesDialog.cs: Added [DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicFields, typeof(T))] to the private chained constructors (where _builder.Connect(this) is called), instructing the NativeAOT trimmer to preserve non-public field reflection metadata for these types.
[DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicFields, typeof(MainWindow))]
private MainWindow(..., Gtk.Builder builder) : base(...)
{
    // ...
    _builder.Connect(this); // now correctly resolves [Gtk.Connect]-annotated fields
    // ...
    _windowTitle!.Title = _appInfo.ShortName; // no longer null
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • crl.godaddy.com
    • Triggering command: /usr/bin/dotnet dotnet restore --no-dependencies /tmp/codeql-scratch-297de09d6e55df2d/dbs/csharp/working/1D28AAEF2CB9C63BCE9DBD21A68FB8D9/missingpackages_workingdir --packages /tmp/codeql-scratch-297de09d6e55df2d/dbs/csharp/working/missingpackages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal --configfile /tmp/codeql-scratch-297de09d6e55df2d/dbs/csharp/working/nugetconfig/nuget.config --force (dns block)
  • https://api.github.com/repos/gircore/gir.core/contents/src/Libs/Gtk-4.0/Public/Builder.cs
    • Triggering command: /usr/bin/curl curl -s REDACTED (http block)
  • ocsp.godaddy.com
    • Triggering command: /usr/bin/dotnet dotnet restore --no-dependencies /tmp/codeql-scratch-297de09d6e55df2d/dbs/csharp/working/1D28AAEF2CB9C63BCE9DBD21A68FB8D9/missingpackages_workingdir --packages /tmp/codeql-scratch-297de09d6e55df2d/dbs/csharp/working/missingpackages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal --configfile /tmp/codeql-scratch-297de09d6e55df2d/dbs/csharp/working/nugetconfig/nuget.config --force (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.

…-public fields for reflection

Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com>
Agent-Logs-Url: https://github.com/NickvisionApps/Application/sessions/236a2817-ce25-4a8f-813a-5f89c86b0623
Copilot AI changed the title [WIP] Fix null reference exception in MainWindow constructor Fix NativeAOT NullReferenceException in GNOME views Mar 21, 2026
Copilot AI requested a review from nlogozzo March 21, 2026 15:33
@nlogozzo nlogozzo marked this pull request as ready for review March 21, 2026 15:36
@nlogozzo nlogozzo merged commit 488a96a into main Mar 21, 2026
17 checks passed
@nlogozzo nlogozzo deleted the copilot/fix-null-reference-exception branch March 21, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants